#include "client.h"


//执行指令
int execute_choose(int choose,struct login_status* stat,int* status,struct client_info* info,int socket_fd)
{
    switch(choose)
    {
    case 0:
        *status=1;
        break;
    case 1:
        {
        printf("请登录>>>>>\n");
        
        int size=0;

        //登录逻辑
        printf("请输入账号:");
        scanf("%s",stat->name);
        getchar();
        printf("请输入密码:");
        scanf("%s",stat->password);
        getchar();
        //合并初始地址
        sscanf(stat->name,"/%s",stat->path);

        info->command=LOGIN;
        
        //拼接protocol

        //验证账号
        send(socket_fd,&info->command,sizeof(int),MSG_NOSIGNAL);
        size=strlen(stat->name);
        send(socket_fd,&size,sizeof(int),MSG_NOSIGNAL);
        send(socket_fd,stat->name,size,MSG_NOSIGNAL);
        send(socket_fd,stat->password,strlen(stat->password),MSG_NOSIGNAL);
        //todo验证账号的正确并接受返回值修改本地结构体参数
        int stats=-1;
        recv(socket_fd,&stats,sizeof(int),MSG_WAITALL);
        if(stats==1)
        {
        stat->login=true;
        printf("%s->登录成功\n",stat->path);
        }
        else
        {
            memset(&stat,0,sizeof(stat));
            printf("用户名或者密码输入错误，请重新输入。\n");
        }
        break;
        }
    case 2:
        {
        printf("请注册>>>>>\n");

        //注册
        printf("请输入账号，小于8位:  ");
        char name[10];
        scanf("%s",name);
        getchar();
        printf("请输入密码,小于18位:  ");
        char password[20];
        scanf("%s",password);
        getchar();
        info->command=REGIST;
        //注册完成之后向服务器发送完成当前注册
        //判断用户名是否重复
        send(socket_fd,&info->command,sizeof(int),MSG_NOSIGNAL);
        int size=0;
        size=strlen(name);
        send(socket_fd,&size,sizeof(int),MSG_NOSIGNAL);
        send(socket_fd,name,size,MSG_NOSIGNAL);
        send(socket_fd,password,strlen(password),MSG_NOSIGNAL);
        int stats=-1;
        recv(socket_fd,&stats,sizeof(int),MSG_WAITALL);
        if(stats==1)
        {
        printf("注册成功\n");
        }
        else 
            printf("注册失败\n");
        break;
        }
    default:
        printf("你的输入有问题,请检查后重新输入\n");
        break;
    }
    return 0;
}

//分割命令
int depart_command(struct client_info* info ,char * msg)
{
    char* command=strtok(msg," ");
    //命令匹配
    ensure_command(command,info);
    
    if(info->command!=ERR)
    {
    //跳过空白
    char* param=strtok(NULL," ");
    
    //把参数线拷贝到info
    memcpy(info->protocol,param,strlen(param));
    }
    return 0;
}




